package com.sophossystems.client.asmon.dao.spring;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.transaction.annotation.Transactional;

import com.sophossystems.client.asmon.dao.ClientePMoralesDao;
import com.sophossystems.client.asmon.dto.ClienteDTO;
import com.sophossystems.client.asmon.dto.ClientePMoralesDTO;
import com.sophossystems.client.asmon.dto.ClientePMoralesPkDTO;
import com.sophossystems.client.asmon.dto.ClientePersonasFisicasDTO;
import com.sophossystems.client.asmon.exceptions.DaoException;
import com.sophossystems.client.asmon.hibernate.ClientePMorales;

public class ClientePMoralesDaoImpl extends AbstractDAO implements  ClientePMoralesDao
{
	
	private static Logger log = Logger.getLogger(ClientePMoralesDaoImpl.class);
	
	
	/**
	 * Method 'insert'
	 * 
	 * @param dto
	 * @return ActivityPk
	 */
	@Override
	@Transactional
	public ClientePMoralesPkDTO insert(final ClientePMoralesDTO dto){
		ClientePMoralesPkDTO exito =null ;
	try {
		 exito = (ClientePMoralesPkDTO) getHibernateTemplate().execute(new HibernateCallback() {
			/**
			 * {@inheritDoc}
			 * 
			 * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
			 */
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
			    Transaction tx = session.beginTransaction();
			    ClientePMoralesPkDTO pk = null;
			    ClientePMorales personaMoral = new ClientePMorales();
			   personaMoral.mapper(dto);
			    session.save(personaMoral);
			    tx.commit();
			    
			    try{
			    	ClientePMoralesPkDTO res = findLastInsert();
			    	pk = new ClientePMoralesPkDTO();
			    	pk.setIdCliente(res.getIdCliente());
			    }catch (DaoException es) {
				
			    	throw new HibernateException("Insersion: " +es.getMessage());
				}

				return pk;
			
				
			}

			
		});
	
	} catch (final Exception e) {
		logger.error("Error: getActivity, cause: " + e.getMessage(), e);
		
	}
	return exito;
}

		
	/** 
	 * Updates a single row in the Activity table.
	 */
	
	@Override
	@Transactional
	public void update(final ClientePMoralesDTO dto) throws DaoException
	{
		try {
			 getHibernateTemplate().execute(new HibernateCallback() {
				/**
				 * {@inheritDoc}
				 * 
				 * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
				 */
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					ClientePMoralesPkDTO pk = null;
					ClientePMoralesDTO res = null;
					Transaction tx = session.beginTransaction();
					// Aqui primero se busca si existe el registro
					try {
						res = findByPrimaryKey(dto
								.getIdCliente());
						pk = new ClientePMoralesPkDTO();
						pk.setIdCliente(res.getIdCliente());
					} catch (DaoException es) {
						throw new HibernateException(
								"Update ActivityDao: "
										+ es.getMessage());
					}
					///aqui se actualizar los campos y con los de la entrada  
					//cuidado de actualiza todo.
					ClientePMorales persis = new ClientePMorales();
					persis.mapper(dto);
					persis.setIdCliente(res.getIdCliente());
					session.update(persis);
					tx.commit();
					return null;
				}		
			});
		} catch (final Exception e) {
			logger.error("Error: getActivity, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getActivity, cause: " + e.getMessage());
		}
	
		
	
	}

	/** 
	 * Deletes a single row in the Activity table.
	 */
	
	@Override
	@Transactional
	public void delete(final ClientePMoralesPkDTO pk) throws DaoException
	{
		
		
		try {
			 getHibernateTemplate().execute(new HibernateCallback() {
				/**
				 * {@inheritDoc}
				 * 
				 * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
				 */
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
				    Transaction tx = session.beginTransaction();
				    ClientePMorales personaMoral = (ClientePMorales)session.load(ClientePMorales.class,
				    		pk.getIdCliente());
				    session.delete(personaMoral);
					tx.commit();
					return null;
				}		
			});
		} catch (final Exception e) {
			logger.error("Error: getActivity, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getActivity, cause: " + e.getMessage());
		}
	}

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 */
	public String getTableName()
	{
		return "ClientePMorales";
	}

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'idCliente = :idCliente'.
	 */
	@Override
	@Transactional
	public ClientePMoralesDTO findByPrimaryKey(final int idCliente)
			throws DaoException	{
		ClientePMoralesDTO clientePersonaM = null;
		try {
			clientePersonaM = (ClientePMoralesDTO)getHibernateTemplate().execute(new
					HibernateCallback() {
						
						@Override
						public Object doInHibernate(Session session) throws HibernateException,
								SQLException {
						  
							final Query oQuery = session.createQuery("SELECT pm.idCliente as idCliente," +
									" pm.razonSocial as razonSocial, pm.inicioActividad as inicioActividad," +
									" pm.tel1 as tel1, pm.ext1 as ext1, pm.tel2 as tel2, pm.ext2 as ext2," +
									" pm.email as email, pm.tipoActividad as tipoActividad," +
									" pm.tipoRegimen as tipoRegimen, pm.antecedentes as antecedentes," +
									" pm.recomienda as recomienda, pm.idDireccion as idDireccion," +
									" pm.idClienteGeneral as idClienteGeneral FROM "
									+ getTableName() + " pm WHERE pm.idCliente = :id_cliente");
							oQuery.setInteger("id_cliente", idCliente);
							ClientePMoralesDTO clientePMoral = (ClientePMoralesDTO) oQuery
									.setResultTransformer(Transformers.aliasToBean(
											ClientePMoralesDTO.class))
											.list().get(0);
							return clientePMoral;
						}
					});
//			List<ClientePMoralesDTO> list = jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE idCliente = ?", this,idCliente);
//			return list.size() == 0 ? null : list.get(0);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return clientePersonaM;
	}

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria ''.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClientePMoralesDTO> findAll() 
			throws DaoException {
		List<ClientePMoralesDTO> clientePersonaM = null;
		try {
			clientePersonaM = (List<ClientePMoralesDTO>)getHibernateTemplate().execute(new
					HibernateCallback() {
						
						@Override
						public Object doInHibernate(Session session) throws HibernateException,
								SQLException {
						  
							final Query oQuery = session.createQuery("SELECT pm.idCliente as idCliente," +
									" pm.razonSocial as razonSocial,pm.inicioActividad as inicioActividad," +
									" pm.tel1 as tel1, pm.ext1 as ext1, pm.tel2 as tel2, pm.ext2 as ext2," +
									" pm.email as email, pm.tipoActividad as tipoActividad," +
									" pm.tipoRegimen as tipoRegimen, pm.antecedentes as antecedentes," +
									" pm.recomienda as recomienda, pm.idDireccion as idDireccion," +
									" pm.idClienteGeneral as idClienteGeneral FROM "
									+ getTableName() + " pm ORDER BY pm.idCliente");
							List<ClientePMoralesDTO> clientePMoral = oQuery
									.setResultTransformer(Transformers.aliasToBean(
											ClienteDTO.class)).list();
							return clientePMoral;
						}
					});
//			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " ORDER BY idCliente", this);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return clientePersonaM;
	}

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'idCliente = :idCliente'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClientePMoralesDTO> findWhereIdClienteEquals(final int idCliente)
			throws DaoException	{
		List<ClientePMoralesDTO> clientePersonaM = null;
		try {
			clientePersonaM = (List<ClientePMoralesDTO>)getHibernateTemplate().execute(new
					HibernateCallback() {
						
						@Override
						public Object doInHibernate(Session session) throws HibernateException,
								SQLException {
						  
							final Query oQuery = session.createQuery("SELECT pm.idCliente as idCliente," +
									" pm.razonSocial as razonSocial,pm.inicioActividad as inicioActividad," +
									" pm.tel1 as tel1, pm.ext1 as ext1, pm.tel2 as tel2, pm.ext2 as ext2," +
									" pm.email as email, pm.tipoActividad as tipoActividad," +
									" pm.tipoRegimen as tipoRegimen, pm.antecedentes as antecedentes," +
									" pm.recomienda as recomienda, pm.idDireccion as idDireccion," +
									" pm.idClienteGeneral as idClienteGeneral FROM "
									+ getTableName() + " pm WHERE pm.idCliente = :id_cliente ORDER BY pm.idCliente");
							oQuery.setInteger(0, idCliente);
							List<ClientePMoralesDTO> clientePMoral = oQuery
									.setResultTransformer(Transformers.aliasToBean(
											ClientePMoralesDTO.class)).list();
							return clientePMoral;
						}
					});
//			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, , tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE idCliente = ? ORDER BY idCliente", this,idCliente);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return clientePersonaM;
	}

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'razonSocial = :razonSocial'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClientePMoralesDTO> findWhereRazonSocialEquals(final String razonSocial)
			throws DaoException	{
		List<ClientePMoralesDTO> clientePersonaM = null;
		try {
			clientePersonaM = (List<ClientePMoralesDTO>)getHibernateTemplate().execute(new
					HibernateCallback() {
						
						@Override
						public Object doInHibernate(Session session) throws HibernateException,
								SQLException {
						  
							final Query oQuery = session.createQuery("SELECT pm.idCliente as idCliente," +
									" pm.razonSocial as razonSocial,pm.inicioActividad as inicioActividad," +
									" pm.tel1 as tel1, pm.ext1 as ext1, pm.tel2 as tel2, pm.ext2 as ext2," +
									" pm.email as email, pm.tipoActividad as tipoActividad," +
									" pm.tipoRegimen as tipoRegimen, pm.antecedentes as antecedentes," +
									" pm.recomienda as recomienda, pm.idDireccion as idDireccion," +
									" pm.idClienteGeneral as idClienteGeneral FROM "
									+ getTableName() + " pm WHERE pm.razonSocial = :razon ORDER BY pm.razonSocial");
							oQuery.setString(0, razonSocial);
							List<ClientePMoralesDTO> clientePMoral = oQuery
									.setResultTransformer(Transformers.aliasToBean(
											ClientePMoralesDTO.class)).list();
							return clientePMoral;
						}
					});
//			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE razonSocial = ? ORDER BY razonSocial", this,razonSocial);
		}catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return clientePersonaM;
	}

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'fInicioActividad = :finicioactividad'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereFinicioactividadEquals(Date finicioactividad) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE fInicioActividad = ? ORDER BY fInicioActividad", this,finicioactividad);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'rfc = :rfc'.
	
	@Transactional
	public List<ClientePMoralesDTO> findWhereRfcEquals(final String rfc)
			throws DaoException {
		List<ClientePMoralesDTO> clientePersonaM = null;
		try {
			clientePersonaM = (List<ClientePMoralesDTO>)getHibernateTemplate().execute(new
					HibernateCallback() {
						
						@Override
						public Object doInHibernate(Session session) throws HibernateException,
								SQLException {
						  
							final Query oQuery = session.createQuery("SELECT idCliente, razonSocial, " +
									"fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad," +
									" tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() +
									 " WHERE rfc = :rfc ORDER BY rfc");
							oQuery.setString(0, rfc);
							List<ClientePMoralesDTO> clientePMoral = (List<ClientePMoralesDTO>) oQuery
									.setResultTransformer(Transformers.aliasToBean(
											ClientePMoralesDTO.class));
							return clientePMoral;
						}
					});
//			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE rfc = ? ORDER BY rfc", this,rfc);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return clientePersonaM;
	} */

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'tel1 = :tel1'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereTel1Equals(int tel1) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE tel1 = ? ORDER BY tel1", this,tel1);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'ext1 = :ext1'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereExt1Equals(int ext1) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE ext1 = ? ORDER BY ext1", this,ext1);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'tel2 = :tel2'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereTel2Equals(int tel2) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE tel2 = ? ORDER BY tel2", this,tel2);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'ext2 = :ext2'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereExt2Equals(int ext2) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE ext2 = ? ORDER BY ext2", this,ext2);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'email = :email'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereEmailEquals(String email) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE email = ? ORDER BY email", this,email);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'tipoActividad = :tipoActividad'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereTipoActividadEquals(String tipoActividad) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE tipoActividad = ? ORDER BY tipoActividad", this,tipoActividad);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'tipoRegimen = :tipoRegimen'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereTipoRegimenEquals(String tipoRegimen) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE tipoRegimen = ? ORDER BY tipoRegimen", this,tipoRegimen);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'antecedentes = :antecedentes'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereAntecedentesEquals(String antecedentes) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE antecedentes = ? ORDER BY antecedentes", this,antecedentes);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'recomienda = :recomienda'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereRecomiendaEquals(String recomienda) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE recomienda = ? ORDER BY recomienda", this,recomienda);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		
	}*/

	/** 
	 * Returns all rows from the CLIENTE_P_MORALES table that match the criteria 'idDireccion = :idDireccion'.
	 
	@Transactional
	public List<ClientePMoralesDTO> findWhereIdDireccionEquals(int idDireccion) throws DaoException
	{
		try {
			return jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, rfc, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE idDireccion = ? ORDER BY idDireccion", this,idDireccion);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
			
		}
		
	}*/

	/** 
	 * Returns the rows from the CLIENTE_P_MORALES table that matches the specified primary-key value.
	 */
	public ClientePMoralesDTO findByPrimaryKey(ClientePMoralesPkDTO pk) throws DaoException
	{
		return findByPrimaryKey( pk.getIdCliente() );
	}

	@Override
	public ClientePMoralesPkDTO findLastInsert() throws DaoException {
		ClientePMoralesPkDTO personaMoral = null;
		try {
			personaMoral = (ClientePMoralesPkDTO) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							
							Query oQuery = session
									.createQuery("SELECT max(c.idCliente) as idCliente FROM "
											+ getTableName()
											+ " c ");
							
							ClientePMoralesPkDTO personaMoralDto = (ClientePMoralesPkDTO) oQuery
									.setResultTransformer(Transformers
											.aliasToBean(ClientePMoralesPkDTO.class)).list().get(0);
							return personaMoralDto;
						}
					});
		
		} catch (Exception e) {
			logger.error("Error: cliente persona moral, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return personaMoral;

	}
	
	public ClientePMoralesDTO findByIdClienteGeneral(final int idClienteGeneral)
			throws DaoException	{
		ClientePMoralesDTO clientePersonaM = null;
		try {
			clientePersonaM = (ClientePMoralesDTO)getHibernateTemplate().execute(new
					HibernateCallback() {
						
						@Override
						public Object doInHibernate(Session session) throws HibernateException,
								SQLException {
						  
							final Query oQuery = session.createQuery("SELECT pm.idCliente as idCliente," +
									" pm.razonSocial as razonSocial, pm.inicioActividad as inicioActividad," +
									" pm.tel1 as tel1, pm.ext1 as ext1, pm.tel2 as tel2, pm.ext2 as ext2," +
									" pm.email as email, pm.tipoActividad as tipoActividad," +
									" pm.tipoRegimen as tipoRegimen, pm.antecedentes as antecedentes," +
									" pm.recomienda as recomienda, pm.idDireccion as idDireccion," +
									" pm.idClienteGeneral as idClienteGeneral FROM "
									+ getTableName() + " pm WHERE pm.idClienteGeneral = :id_cliente");
							oQuery.setInteger("id_cliente", idClienteGeneral);
							ClientePMoralesDTO clientePMoral = (ClientePMoralesDTO) oQuery
									.setResultTransformer(Transformers.aliasToBean(
											ClientePMoralesDTO.class))
											.list().get(0);
							return clientePMoral;
						}
					});
//			List<ClientePMoralesDTO> list = jdbcTemplate.query("SELECT idCliente, razonSocial, fInicioActividad, tel1, ext1, tel2, ext2, email, tipoActividad, tipoRegimen, antecedentes, recomienda, idDireccion FROM " + getTableName() + " WHERE idCliente = ?", this,idCliente);
//			return list.size() == 0 ? null : list.get(0);
		}
		catch (Exception e) {
			logger.error("Error: CLIENTE_P_MORALES, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return clientePersonaM;
	}
	
	@SuppressWarnings("unchecked")
	public List<ClientePMoralesDTO> busquedaPorPalabra(final String palabra) throws DaoException{
		List<ClientePMoralesDTO> listaClientes = null;
		try {
			listaClientes = (List<ClientePMoralesDTO>) getHibernateTemplate().execute(new HibernateCallback() {
				
				@Override
				public Object doInHibernate(Session session) throws HibernateException,
						SQLException {
					Query oQuery = session.createQuery("SELECT pm.razonSocial as razonSocial," +
							" pm.idClienteGeneral as idClienteGeneral FROM "
							+ getTableName() + " pm WHERE pm.razonSocial like :palabraBusqueda");
					oQuery.setString("palabraBusqueda", "%" + palabra + "%");
					List<ClientePMoralesDTO> listaResultado = oQuery.setResultTransformer(Transformers.
							aliasToBean(ClientePMoralesDTO.class)).list();
					
					return listaResultado;
				}
			});
		} catch (Exception e) {
			 log.debug("Error en busqueda por palabra, causa: " + e.getCause());
		}
		return listaClientes;
	}

	
}
